* **************************************************************************** *
*  LEAN REPLICATION SCRIPT - Allemand et al.                                  *
*  "Conscientiousness in the workplace"                                      *
*  Produces only outputs included in final paper                              *
*  Created from original analysis script                                      *
*                                                                              *
********************************************************************************

********************************************************************************
*  LOAD AND PREPARE DATA
********************************************************************************

use "$data/2_processed/panel_analysis.dta", clear

global seed = 156
set seed $seed

********************************************************************************
*  GLOBAL MACROS FOR VARIABLE LISTS
********************************************************************************

// Control variables
global X0_base sex_base age_base educ_high_base hh_n_bedrooms_base hh_n_children_base hh_n_above_18_base
global X1_mid_base total_debt_mid_base total_save_mid_base hhincome_mid_base

// Main outcome variables
global Y_labor employed eif_still_work
global Y_labor_2 whyleft_endcontract3

global Y_earn inter_earnlstmonth_mid 
global Y_earn_base inter_earnlstmonth_mid_base

global Y_earn_2 hourly_wage 
global Y_earn_2_base hourly_wage_base

global Y_earn_3 grade_2019_admin

// Alternative earnings variables
global Y_earn_alt_1 inter_earnlstmonth_low inter_earnlstmonth_top 
global Y_earn_alt_2 inter_earnlstmonth_mid_log
global Y_earn_alt_3 inter_earnlstmonth_mid_ihs
global Y_earn_alt_2_base inter_earnlstmonth_mid_log_base
global Y_earn_alt_3_base inter_earnlstmonth_mid_ihs_base

// Conscientiousness trait groups (consistent across all analyses)
global traits_org trait_tidy trait_follow_schedule trait_followup_method ///
                  trait_organize_schedule trait_clean_up trait_orga_pers_spaces ///
                  trait_keep_organized trait_keep_tidy

global traits_ind trait_make_plan trait_work_goals trait_set_clear_goals ///
                  trait_set_high_standards trait_change_setback trait_work_hard ///
                  trait_focus_goals trait_make_plan_for_goal trait_keep_trying_if_fail

global traits_res trait_attentive_others trait_others_dependent trait_keep_promises ///
                  trait_fulfill_promises trait_up_to_responsib trait_respect_engagements ///
                  trait_others_count trait_fulfill_duty trait_manage_responsib

global traits_pun trait_remorse_late trait_earlier_meet trait_avoid_late ///
                  trait_avoid_procrast trait_on_time trait_late_meetings

global traits_avg trait_avg_orga trait_avg_indus trait_avg_resp trait_avg_punc

// All conscientiousness traits combined
global traits_all $traits_org $traits_ind $traits_res $traits_pun

global traits_all_base trait_tidy_base trait_follow_schedule_base trait_followup_method_base ///
    trait_organize_schedule_base trait_clean_up_base trait_orga_pers_spaces_base ///
    trait_keep_organized_base trait_keep_tidy_base trait_make_plan_base trait_work_goals_base ///
    trait_set_clear_goals_base trait_set_high_standards_base trait_change_setback_base ///
    trait_work_hard_base trait_focus_goals_base trait_make_plan_for_goal_base ///
    trait_keep_trying_if_fail_base trait_attentive_others_base trait_others_dependent_base ///
    trait_keep_promises_base trait_fulfill_promises_base trait_up_to_responsib_base ///
    trait_respect_engagements_base trait_others_count_base trait_fulfill_duty_base ///
    trait_manage_responsib_base trait_remorse_late_base trait_earlier_meet_base ///
    trait_avoid_late_base trait_avoid_procrast_base trait_on_time_base trait_late_meetings_base

// Big 5
global big5_avg big5_avg_extra big5_avg_agreea big5_avg_emoti big5_avg_open
global big5_avg_base big5_avg_extra_base big5_avg_agreea_base big5_avg_emoti_base big5_avg_open_base

// COVID-19 outcome variables
global covid_outcomes covid_meas_count covid_effect_meas_count covid_any_sympt covid_change_work_any ///
    covid_hhm_lost_job covid_feb_revenue_lower covid_feb_tot_revenue_lower ///
    covid_feb_remittances_lower covid_feb_food_exp_higher covid_feb_health_exp_higher ///
    covid_feb_edu_exp_higher covid_feb_transf_abroad_higher covid_feb_savings_lower ///
    covid_feb_debt_higher

// Balance table variables  
global balance_labor inter_earnlstmonth_mid
global balance_controls sex age educ_high first_formal recent_migr
global balance_controls_hh hh_n_bedrooms hh_n_children hh_n_above_18 total_debt_mid total_save_mid hhincome_mid
global balance_big5 big5_avg_consc big5_avg_extra big5_avg_agreea big5_avg_emoti big5_avg_open

********************************************************************************
*  MAIN TEXT TABLE 1: EMPLOYMENT AND RETENTION
********************************************************************************

eststo clear

foreach y in $Y_labor {
    eststo: reg `y' treat if survey_round==3, robust
    scalar beta_unstd = _b[treat]
    sum `y' if (survey_round == 3 & treat == 0)
    scalar sigma_y = r(sd)
    scalar std_y=beta_unstd / sigma_y
    scalar mean_y = r(mean)
        estadd scalar StdY = round(std_y, 0.001), replace
        estadd scalar Mean = round(mean_y, 0.01), replace
        estadd local Base "", replace
                    
    eststo: reg `y' treat $X0_base $X1_mid_base if survey_round==3, robust
    scalar beta_unstd = _b[treat]
    sum `y' if (survey_round == 3 & treat == 0)
    scalar sigma_y = r(sd)
    scalar std_y=beta_unstd / sigma_y
        estadd scalar StdY = round(std_y, 0.001), replace
        estadd scalar Mean = round(r(mean), 0.01), replace
        estadd local Base "\checkmark", replace
}

eststo: reg $Y_labor_2 treat if survey_round==3 & eif_still_work==0 & last7d_working==1, robust
scalar beta_unstd = _b[treat]
sum $Y_labor_2 if (survey_round == 3&eif_still_work==0&last7d_working==1 & treat == 0)
scalar sigma_y = r(sd)
scalar std_y=beta_unstd / sigma_y
scalar mean_y = r(mean)
    estadd scalar StdY = round(std_y, 0.001), replace
    estadd scalar Mean = round(mean_y, 0.01), replace
    estadd local Base "", replace
                
eststo: reg $Y_labor_2 treat $X0_base $X1_mid_base if survey_round==3 & eif_still_work==0 & last7d_working==1, robust
scalar beta_unstd = _b[treat]
sum $Y_labor_2 if (survey_round == 3 & eif_still_work==0 & last7d_working==1 & treat == 0)
scalar sigma_y = r(sd)
scalar std_y=beta_unstd / sigma_y
    estadd scalar StdY = round(std_y, 0.001), replace
    estadd scalar Mean = round(r(mean), 0.01), replace
    estadd local Base "\checkmark", replace

esttab * using "$outputs/table1_labour-market.tex", ///
    replace compress nocons se varlabels(_cons "Constant" treat "Treated") ///
    title("Table 1: Employment and retention") ///
    drop(sex_base age_base educ_high_base hh_n_bedrooms_base hh_n_children_base hh_n_above_18_base total_* hhincome*) ///
    booktabs star(* 0.10 ** 0.05 *** 0.01 ) nonotes b(3) se(3) ///
    s(StdY Mean Base N r2, fmt(%9.3f %9.3f %9s %9.0f %9.3f) ///
    label("Standardized coeff." "Mean outcome control" "Baseline controls" "N" "R-sq")) ///
    mtitles("Employed" "Employed" "Still at company" "Still at company" "Contract not renewed" "Contract not renewed")

eststo clear


********************************************************************************
*  MAIN TEXT TABLE 2: EARNINGS
********************************************************************************

eststo clear

* -------- $Y_earn: Inter-earnings last month -------- *

eststo: reg $Y_earn treat if survey_round==3, robust
scalar beta_unstd = _b[treat]

sum $Y_earn if (survey_round == 3 & treat == 0)
scalar sigma_y = r(sd)
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd local Base "", replace
    estadd local BaseO "", replace
    estadd scalar StdY = beta_unstd/sigma_y, replace
            
eststo: reg $Y_earn treat $X0_base $X1_mid_base if survey_round==3, robust
scalar beta_unstd = _b[treat]

sum $Y_earn if (survey_round == 3 & treat == 0)
scalar sigma_y = r(sd)
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd local Base "\checkmark", replace
    estadd local BaseO "", replace
    estadd scalar StdY = beta_unstd/sigma_y, replace
            
eststo: reg $Y_earn treat $X0_base $X1_mid_base $Y_earn_base if survey_round==3, robust
scalar beta_unstd = _b[treat]

sum $Y_earn if (survey_round == 3 & treat == 0)
scalar sigma_y = r(sd)
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd local Base "\checkmark", replace
    estadd local BaseO "\checkmark", replace
    estadd scalar StdY = beta_unstd/sigma_y, replace

eststo: reg $Y_earn treat $X0_base $X1_mid_base $Y_earn_base if survey_round==3 & $Y_earn > 0, robust
scalar beta_unstd = _b[treat]

sum $Y_earn if (survey_round == 3 & treat == 0 & $Y_earn > 0)
scalar sigma_y = r(sd)
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd local Base "\checkmark", replace
    estadd local BaseO "\checkmark", replace
    estadd scalar StdY = beta_unstd/sigma_y, replace

* ------------ $Y_earn_2: Hourly wage ------------ *

eststo: reg $Y_earn_2 treat $X0_base $X1_mid_base $Y_earn_2_base if survey_round==3 & inter_earnlstmonth_mid > 0, robust
scalar beta_unstd = _b[treat]

sum $Y_earn_2 if (survey_round == 3 & treat == 0)
scalar sigma_y = r(sd)
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd local Base "\checkmark", replace
    estadd local BaseO "\checkmark", replace
    estadd scalar StdY = beta_unstd/sigma_y, replace

* ------------ $Y_earn_3: Grade 2019 ------------ *

eststo: reg $Y_earn_3 treat $X0_base $X1_mid_base if survey_round==3, robust
scalar beta_unstd = _b[treat]

sum $Y_earn_3 if (survey_round == 3 & treat == 0)
scalar sigma_y = r(sd)
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd local Base "\checkmark", replace
    estadd local BaseO "", replace
    estadd scalar StdY = beta_unstd/sigma_y, replace

* ------------ Build table ------------ *

esttab * using "$outputs/table2_earnings.tex", replace ///
    compress nocons se varlabels(_cons "Constant" treat "Treated" eif_still_work "Still at company") ///
    title("Table 2: Earnings and performance") ///
    drop(sex_base age_base educ_high_base hh_n_bedrooms_base hh_n_children_base hh_n_above_18_base total_* hhincome* inter_* hourly_*) ///
    booktabs star(* 0.10 ** 0.05 *** 0.01 ) nonotes ///
    s(StdY Mean Base BaseO N r2, fmt(%9.3f %9.3f %9s %9s %9.0f %9.3f) ///
    label("Standardized coeff." "Mean outcome control" "Baseline controls" "Baseline outcome" "N" "R-sq")) ///
    mtitles("Earnings" "Earnings" "Earnings" "Earnings" "Hourly Wage" "Hourly Wage")

eststo clear

********************************************************************************
*  FIGURE 2: BASELINE EARNINGS DISTRIBUTION  
********************************************************************************

preserve

foreach trait in $traits_all $traits_avg {
	gen treat_`trait' = treat
	local varlab: variable label `trait'
	local varlab: subinstr local varlab "Trait: " "", all
	label var treat_`trait' "`varlab'"
	
	quiet: reg `trait' treat_`trait' $X0_base $X1_mid_base `trait'_base if survey_round==2
	eststo `trait'
}

#delimit ;
coefplot $traits_all $traits_avg, keep(treat_*) nokey xline(0) label coeflabels(, labsize(small)) 
	levels(90)
	headings(treat_trait_tidy = "{bf:Organizational}" 
			treat_trait_make_plan = "{bf:Industriousness}" 
			treat_trait_attentive_others = "{bf:Responsibility}" 
			treat_trait_remorse_late = "{bf:Punctuality}"
			treat_trait_avg_orga = "{bf:Averages}") 
	ciopts(color(navy)) mcolor(navy) scale(*0.8) 
	;
#delimit cr
graph export "$outputs/figure2_coefplot-conscien-items.png", replace

restore

********************************************************************************
*  FOOTNOTES
********************************************************************************

* -------- Employed and still at company at endline (fn.21) -------- *
sum employed eif_still_work if survey_round == 3

* -------- Labour market otucomes, Logit (fn.22) -------- *

logit	employed i.treat if survey_round==3
margins treat, atmeans

logit	employed i.treat $X0_base $X1_mid_base if survey_round==3
margins treat, atmeans

logit	eif_still_work i.treat if survey_round==3
margins treat, atmeans

logit	eif_still_work i.treat $X0_base $X1_mid_base if survey_round==3
margins treat, atmeans

* -------- Earnings outcome averages (fn.25) -------- *

sum inter_earnlstmonth_mid if (survey_round == 3)
sum inter_earnlstmonth_mid if (survey_round == 3 & inter_earnlstmonth_mid>0)


********************************************************************************
*  APPENDIX OUTPUTS B
********************************************************************************

* ------------ Table B.1 ------------ *
* Sample characteristics at baseline for attrition vs non-attrition groups with Cohen's D

iebaltab $balance_labor $balance_controls $balance_big5 $balance_controls_hh ///
    if survey_round==1 & sample==1, ///
    groupvar(attritor) savexlsx("$outputs/tableB1_balance-attrit_cohen.xlsx") ///
    format(%12.3gc) rowvarlabels total tblnonote replace

local i = 0

foreach v in $balance_labor $balance_controls $balance_big5 $balance_controls_hh {
    esize two `v' if survey_round == 1 & sample==1, by(attritor) cohensd
    mat U = (r(d), r(lb_d), r(ub_d))
    
    if `i' == 0 {
        mat T = U
    }
    else {
        mat empty_row = (., ., .)
        mat T = T \ empty_row \ U
    }
    local i = `i'+1
}

mat li T

mat rownames T = $balance_labor $balance_controls $balance_big5
mat colnames T = cohensD upperBound lowerBound
mata {
    T_mata = st_matrix("T")
    T_rounded = round(T_mata, 0.001)
    st_matrix("T", T_rounded)
}

mat li T

*** Export to Excel, then export to LaTeX using the excel2latex plugin ***
putexcel set "$outputs/tableB1_balance-attrit_cohen.xlsx", modify

// putexcel A1 = "Sample characteristics at baseline for attrition vs non-attrition groups with Cohen's D"
putexcel J2 = "Cohen's D"
putexcel K2 = "95% CI"
putexcel K2:L2, merge hcenter
putexcel J4 = matrix(T)
putexcel A4 = "Last earnings"
putexcel A6 = "Female"
putexcel A8 = "Age"
putexcel A10 = "Completed middle school"
putexcel A12 = "First formal job"
putexcel A14 = "Recent migrant"
putexcel A16 = "Big 5: Conscientiousness"
putexcel A18 = "Big 5: Extraversion"
putexcel A20 = "Big 5: Agreeableness"
putexcel A22 = "Big 5: Emotional stability"
putexcel A24 = "Big 5: Openness"
putexcel A26 = "\# Beds in household"
putexcel A28 = "\# Children in household"
putexcel A30 = "\# Adults in household"
putexcel A32 = "Household debt"
putexcel A34 = "Household savings"
putexcel A36 = "Household income"

putexcel close

* Multiple hypothesis testing, note in table
mhtexp $balance_labor $balance_controls $balance_big5 $balance_controls_hh if survey_round == 1 & sample==1, treatment(attritor)

* ------------ Table B.2 ------------ *
* Sample characteristics at baseline with Cohen's D

iebaltab $balance_labor $balance_controls $balance_big5 $balance_controls_hh ///
    if survey_round==1 & sample==1, ///
    groupvar(treat) savexlsx("$outputs/tableB2_balance-treat_cohen.xlsx") ///
    format(%12.3gc) rowvarlabels total tblnonote replace 

local i = 0

foreach v in $balance_labor $balance_controls $balance_big5 $balance_controls_hh {
    esize two `v' if survey_round == 1 & sample==1, by(treat) cohensd
    mat U = (r(d), r(lb_d), r(ub_d))
    
    if `i' == 0 {
        mat T = U
    }
    else {
        mat empty_row = (., ., .)
        mat T = T \ empty_row \ U
    }
    local i = `i'+1
}

mat li T

mat rownames T = $balance_labor $balance_controls $balance_big5
mat colnames T = cohensD upperBound lowerBound
mata {
    T_mata = st_matrix("T")
    T_rounded = round(T_mata, 0.001)
    st_matrix("T", T_rounded)
}

mat li T

*** Export to Excel, then export to LaTeX using the excel2latex plugin ***
putexcel set "$outputs/tableB2_balance-treat_cohen.xlsx", modify

// putexcel A1 = "Sample characteristics at baseline for attrition vs non-attrition groups with Cohen's D"
putexcel J2 = "Cohen's D"
putexcel K2 = "95% CI"
putexcel K2:L2, merge hcenter
putexcel J4 = matrix(T)
putexcel A4 = "Last earnings"
putexcel A6 = "Female"
putexcel A8 = "Age"
putexcel A10 = "Completed middle school"
putexcel A12 = "First formal job"
putexcel A14 = "Recent migrant"
putexcel A16 = "Big 5: Conscientiousness"
putexcel A18 = "Big 5: Extraversion"
putexcel A20 = "Big 5: Agreeableness"
putexcel A22 = "Big 5: Emotional stability"
putexcel A24 = "Big 5: Openness"
putexcel A26 = "\# Beds in household"
putexcel A28 = "\# Children in household"
putexcel A30 = "\# Adults in household"
putexcel A32 = "Household debt"
putexcel A34 = "Household savings"
putexcel A36 = "Household income"

putexcel close

* Multiple hypothesis testing, note in table
mhtexp $balance_labor $balance_controls $balance_big5 $balance_controls_hh if survey_round == 1 & sample==1, treatment(treat)

* ------------ Table B.3 ------------ *
* Correction for multiple inference of the treatment effects

// Run regressions and store p-values
// Bivariate specifications
set seed 156

reg employed treat if survey_round==3 & sample==1, robust
test treat
local biv_p_employed = r(p)

reg eif_still_work treat if survey_round==3 & sample==1, robust  
test treat
local biv_p_still = r(p)

reg inter_earnlstmonth_mid treat if survey_round==3 & sample==1, robust
test treat
local biv_p_earnings = r(p)

// Baseline controls specifications
reg employed treat $X0_base $X1_mid_base if survey_round==3 & sample==1, robust
test treat
local base_p_employed = r(p)

reg eif_still_work treat $X0_base $X1_mid_base if survey_round==3 & sample==1, robust
test treat
local base_p_still = r(p)

reg inter_earnlstmonth_mid treat $X0_base $X1_mid_base inter_earnlstmonth_mid_base if survey_round==3 & sample==1, robust
test treat
local base_p_earnings = r(p)

// Run Westfall-Young corrections and capture results
wyoung employed eif_still_work inter_earnlstmonth_mid, cmd(reg OUTCOMEVAR treat if survey_round==3 & sample==1, r) familyp(treat) bootstraps(1000) seed($seed)
matrix biv_wy = r(table)

wyoung employed eif_still_work, cmd(reg OUTCOMEVAR treat $X0_base $X1_mid_base if survey_round==3 & sample==1, r) familyp(treat) bootstraps(1000) seed($seed)
matrix base_wy = r(table)

wyoung inter_earnlstmonth_mid, cmd(reg OUTCOMEVAR treat $X0_base $X1_mid_base inter_earnlstmonth_mid_base if survey_round==3 & sample==1, r) familyp(treat) bootstraps(1000) seed($seed)
matrix base2_wy = r(table)


// Extract matrix values to locals
local biv_wy_employed = biv_wy[1,4]
local biv_wy_still = biv_wy[2,4]
local biv_wy_earnings = biv_wy[3,4]

local base_wy_employed = base_wy[1,4]
local base_wy_still = base_wy[2,4] 
local base_wy_earnings = base2_wy[1,4]

// Create formatted table using putexcel
putexcel set "$outputs/tableB3_multiple-testing.xlsx", replace

putexcel A1 = "Correction for multiple inference of the treatment effects"
putexcel B3 = "Bivariate"
putexcel C3 = "Baseline controls"

putexcel A5 = "Employed"
putexcel A6 = "Unadjusted p-value" 
putexcel B6 = `biv_p_employed', nformat(0.000)
putexcel C6 = `base_p_employed', nformat(0.000)
putexcel A7 = "Westfall and Young (1993) adjusted p-value"
putexcel B7 = `biv_wy_employed', nformat(0.000)
putexcel C7 = `base_wy_employed', nformat(0.000)

putexcel A9 = "Still at company"
putexcel A10 = "Unadjusted p-value"
putexcel B10 = `biv_p_still', nformat(0.000)
putexcel C10 = `base_p_still', nformat(0.000)
putexcel A11 = "Westfall and Young (1993) adjusted p-value"
putexcel B11 = `biv_wy_still', nformat(0.000)
putexcel C11 = `base_wy_still', nformat(0.000)

putexcel A13 = "Monthly earnings"
putexcel A14 = "Unadjusted p-value"
putexcel B14 = `biv_p_earnings', nformat(0.000)
putexcel C14 = `base_p_earnings', nformat(0.000)
putexcel A15 = "Westfall and Young (1993) adjusted p-value"
putexcel B15 = `biv_wy_earnings', nformat(0.000)
putexcel C15 = `base_wy_earnings', nformat(0.000)

putexcel close

* ----------------- Table B.4 ----------------- *
* Occupations at baseline of workers still employed in the construction company vs workers not employed in the company at end-line

iebaltab pos_* if survey_round == 1&sample==1 , ///
    grpvar(eif_still_work_end) savexlsx("$outputs/tableB4_occupations-atbaseline-retained.xlsx") ///
    rowvarlabels tblnonote replace

ta eif_pos eif_still_work_end if survey_round == 1 & eif_pos != 11, ///
    nolabel matcell(tabul)

putexcel set "$outputs/tableB4_occupations-atbaseline-retained.xlsx", modify

forval i = 1/10 {
    
    local excel_row = 2 * `i' + 3
    
    local val_0 = tabul[`i', 1]
    local val_1 = tabul[`i', 2]
    
    putexcel B`excel_row' = `"[`val_0']"'
    putexcel D`excel_row' = `"[`val_1']"'
}

putexcel close

* ----------------- Table B.5 ----------------- *
* Activity sector for workers who left the construction company

eststo	clear

estpost ta currwork_sector if eif_still_work_end == 0 & sample==1
eststo

esttab * using "$outputs/tableB5_jobs-left-company-sector.tex", replace ///
    title("Activity sector for workers who left the construction company") ///
    cells("b(label(Freq.)) pct(fmt(2) label(Pct.))") ///
    varlabel(`e(labels)', blist(Total "\midrule ")) ///
    booktabs compress noobs nomtitle nonum nostar
eststo clear

********************************************************************************
*  APPENDIX OUTPUTS C
********************************************************************************

* ----------------- Figure C.1 ----------------- *
* Distribution of earnings at baseline

histogram inter_earnlstmonth_mid if survey_round == 1&sample==1, ///
    xtitle("Earnings") title("Baseline Earnings") percent graphregion(fcolor(white)) plotregion(fcolor(white)) scheme(s2mono)

graph export "$outputs/figureC1_histogram-earnings.png", replace

* ----------------- Table C.1 ----------------- *
* Alternative earnings specifications

* $Y_earn_alt_1 : inter_earnlstmonth_low, inter_earnlstmonth_top -------

eststo	clear
foreach y in $Y_earn_alt_1{
        
    eststo:	reg	`y' treat $X0_base $X1_mid_base `y'_base if survey_round==3, robust
    listcoef
    mat A = r(table)
    sum	`y' if (survey_round == 3 & treat == 0)
        estadd local Mean = round(`r(mean)', 0.01), replace
        estadd local Base "\checkmark", replace
        estadd local BaseO "\checkmark", replace

    eststo:	reg	`y' treat $X0_base $X1_mid_base `y'_base if survey_round==3 & `y' > 0 , robust
    listcoef
    mat A = r(table)
    sum	`y' if (survey_round == 3 & treat == 0 & `y' > 0)
        estadd local Mean = round(`r(mean)', 0.01), replace
        estadd local Base "\checkmark", replace
        estadd local BaseO "\checkmark", replace

}

* $Y_earn_alt_2 : inter_earnlstmonth_mid_log -------

eststo:	reg	$Y_earn_alt_2 treat if survey_round==3, robust
listcoef
mat A = r(table)
sum	$Y_earn_alt_2 if (survey_round == 3 & treat == 0)
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd local Base "", replace
    estadd local BaseO "", replace
    
eststo:	reg	$Y_earn_alt_2 treat $X0_base $X1_mid_base if survey_round==3, robust
listcoef
mat A = r(table)
sum	$Y_earn_alt_2 if (survey_round == 3 & treat == 0)
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd local Base "\checkmark", replace
    estadd local BaseO "", replace
    
eststo:	reg	$Y_earn_alt_2 treat $X0_base $X1_mid_base $Y_earn_alt_2_base if survey_round==3, robust
listcoef
mat A = r(table)
sum	$Y_earn_alt_2 if (survey_round == 3 & treat == 0)
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd local Base "\checkmark", replace
    estadd local BaseO "\checkmark", replace

* $Y_earn_alt_3 : inter_earnlstmonth_mid_ihs -------

eststo:	reg	$Y_earn_alt_3 treat if survey_round==3, robust
listcoef
mat A = r(table)
sum	$Y_earn_alt_3 if (survey_round == 3 & treat == 0)
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd local Base "", replace
    estadd local BaseO "", replace
    
eststo:	reg	$Y_earn_alt_3 treat $X0_base $X1_mid_base if survey_round==3, robust
listcoef
mat A = r(table)
sum	$Y_earn_alt_3 if (survey_round == 3 & treat == 0)
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd local Base "\checkmark", replace
    estadd local BaseO "", replace
    
eststo:	reg	$Y_earn_alt_3 treat $X0_base $X1_mid_base $Y_earn_alt_3_base if survey_round==3, robust
listcoef
mat A = r(table)
sum	$Y_earn_alt_3 if (survey_round == 3 & treat == 0)
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd local Base "\checkmark", replace
    estadd local BaseO "\checkmark", replace

eststo:	reg	$Y_earn_alt_3 treat $X0_base $X1_mid_base $Y_earn_alt_3_base if survey_round==3 & $Y_earn_alt_3 > 0 , robust
listcoef
mat A = r(table)
sum	$Y_earn_alt_3 if (survey_round == 3 & treat == 0 & $Y_earn_alt_3 > 0)
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd local Base "\checkmark", replace
    estadd local BaseO "\checkmark", replace

* Build table ---------

esttab * using "$outputs/tableC1_earnings-alternative.tex", replace ///
    compress nocons se varlabels(_cons "Constant" treat "Treated" eif_still_work "Still at company") ///
    title("Table C.1: Alternative earnings specifications") ///
    drop(sex_base age_base educ_high_base hh_n_bedrooms_base hh_n_children_base hh_n_above_18_base total_* hhincome* inter_*) ///
    booktabs star(* 0.10 ** 0.05 *** 0.01 ) nonotes ///
    s(Mean Base BaseO N r2,  fmt(%9.3f %9s %9s %9.0f %9.3f) ///
	label("Mean outcome control" "Baseline controls" "Baseline outcome" "N" "R-sq")) ///
    mtitles("Earnings (Min)" "Earnings (Min)" "Earnings (Max)" "Earnings (Max)" "Log earnings" "Log earnings" "Log earnings" ///
            "IHS earnings" "IHS earnings" "IHS earnings" "IHS earnings")

eststo clear

********************************************************************************
*  APPENDIX OUTPUTS D
********************************************************************************

* ----------------- Table D.2 ----------------- *
* Conscientiousness traits: reliability of scales

putexcel set "$outputs/tableD2_consci-traits_reliability.xlsx", replace

putexcel A1 = "Scale" B1 = "Round" ///
    C1 = "Cronbach's alpha" D1 = "Interitem covariance" ///
    E1 = "Number of items"

*----- Overall -----*

*** Baseline ***
alpha $traits_all if survey_round == 1 & sample==1, item

putexcel A2 = "All traits" B2 = "Baseline" ///
    C2 = `r(alpha)' D2 = `r(cov)', nformat(0.###)
putexcel E2 = `r(k)'

*** Midline ***
alpha $traits_all if survey_round == 2 & sample==1, item

putexcel B3 = "Midline" ///
    C3 = `r(alpha)' D3 = `r(cov)', nformat(0.###)
putexcel E3 = `r(k)'

*----- Organisation -----*

*** Baseline ***
alpha $traits_org if survey_round == 1 & sample==1, item

putexcel A4 = "Organisational" B4 = "Baseline" ///
    C4 = `r(alpha)' D4 = `r(cov)', nformat(0.###)
putexcel E4 = `r(k)'

*** Midline ***
alpha $traits_org if survey_round == 2 & sample==1, item

putexcel B5 = "Midline" ///
    C5 = `r(alpha)' D5 = `r(cov)', nformat(0.###)
putexcel E5 = `r(k)'

*----- Industriousness -----*

*** Baseline ***
alpha $traits_ind if survey_round == 1 & sample==1, item

putexcel A6 = "Industriousness" B6 = "Baseline" ///
    C6 = `r(alpha)' D6 = `r(cov)', nformat(0.###)
putexcel E6 = `r(k)'

*** Midline ***
alpha $traits_ind if survey_round == 2 & sample==1, item

putexcel B7 = "Midline" ///
    C7 = `r(alpha)' D7 = `r(cov)', nformat(0.###)
putexcel E7 = `r(k)'

*----- Responsibility -----*

*** Baseline ***
alpha $traits_res if survey_round == 1 & sample==1, item

putexcel A8 = "Responsibility" B8 = "Baseline" ///
    C8 = `r(alpha)' D8 = `r(cov)', nformat(0.###)
putexcel E8 = `r(k)'

*** Midline ***
alpha $traits_res if survey_round == 2 & sample==1, item

putexcel B9 = "Midline" ///
    C9 = `r(alpha)' D9 = `r(cov)', nformat(0.###)
putexcel E9 = `r(k)'

*----- Punctuality -----*

*** Baseline ***
alpha $traits_pun if survey_round == 1 & sample==1, item

putexcel A10 = "Punctuality" B10 = "Baseline" ///
    C10 = `r(alpha)' D10 = `r(cov)', nformat(0.###)
putexcel E10 = `r(k)'

*** Midline ***
alpha $traits_pun if survey_round == 2 & sample==1, item

putexcel B11 = "Midline" ///
    C11 = `r(alpha)' D11 = `r(cov)', nformat(0.###)
putexcel E11 = `r(k)'

putexcel close

* ------------------ WYoung adjusted p-values ----------------- *

wyoung $traits_all $big5_avg, cmd(reg OUTCOMEVAR treat $X0_base $X1_mid_base CONTROLVARS if survey_round==2 & sample==1, r) familyp(treat) controls($traits_all_base $big5_avg_base) bootstraps(1000) seed($seed)
matrix wy_pvals = r(table)

* ----------------- Table D.3 ----------------- *
* Conscientiousness traits: Organisational skill and Industriousness
	
eststo clear

// Store organizational traits
local col = 1
foreach y in $traits_org {
    eststo org_`col': reg `y' treat $X0_base $X1_mid_base `y'_base if survey_round==2&sample==1, robust
    
    listcoef
    mat A = r(table)
    sum `y' if (survey_round == 2 & treat == 0 & sample==1)
    
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd scalar StdY = A[1,5], replace
    estadd local wy_pval = "[" + string(wy_pvals[`col',4], "%9.3f") + "]", replace
    
    local col = `col' + 1
}

// Store industriousness traits
local org_traits_count = 8
local col = 1
foreach y in $traits_ind {
    local wy_index = `org_traits_count' + `col'
    
    eststo ind_`col': reg `y' treat $X0_base $X1_mid_base `y'_base if survey_round==2&sample==1, robust
    
    listcoef
    mat A = r(table)
    sum `y' if (survey_round == 2 & treat == 0 & sample==1)
    
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd scalar StdY = A[1,5], replace
    estadd local wy_pval = "[" + string(wy_pvals[`wy_index',4], "%9.3f") + "]", replace
    
    local col = `col' + 1
}

local indu_traits_count = `col'-1

* Assemble table --------

esttab org_* using "$outputs/tableD3_cosnci-orga-indus.tex", fragment replace ///
    prehead("{" "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" ///
            "\begin{tabular*}{2\textwidth}{l*{`org_traits_count'}{c}}" "\toprule" "\multicolumn{@span}{l}{Panel A: Organisational skill} \\") ///
    postfoot("\end{tabular*}") ///
    compress nocons se varlabels(_cons "Constant" treat "Treated") ///
    drop(trait_*_base sex_base age_base educ_high_base hh_n_bedrooms_base hh_n_children_base hh_n_above_18_base total_* hhincome* ) ///
    booktabs star(* 0.10 ** 0.05 *** 0.01 ) nonotes b(3) se(3) ///
    s(wy_pval StdY Mean N r2, fmt(%9s %9.3f %9.2f %9.0f %9.3f) ///
	label("WY adj. p-value" "Standardized coeff." "Mean outcome control" "N" "R-sq")) ///
    mtitles("Tidy" "Follow schedule" "Follow-up" "Organize schedule" "Clean up" "Organized personal space" "Keep organized" "Keep tidy")

esttab ind_* using "$outputs/tableD3_cosnci-orga-indus.tex", fragment append ///
	prehead("\begin{tabular*}{2\textwidth}{l*{`indu_traits_count'}{c}}" "\toprule" "\multicolumn{@span}{l}{Panel B: Industriousness} \\") ///
    postfoot("\bottomrule" "\end{tabular*}" "}") ///
    compress nocons se varlabels(_cons "Constant" treat "Treated") ///
    drop(trait_*_base sex_base age_base educ_high_base hh_n_bedrooms_base hh_n_children_base hh_n_above_18_base total_* hhincome* ) ///
    booktabs star(* 0.10 ** 0.05 *** 0.01 ) nonotes b(3) se(3) ///
    s(wy_pval StdY Mean N r2, fmt(%9s %9.3f %9.2f %9.0f %9.3f) ///
	label("WY adj. p-value" "Standardized coeff." "Mean outcome control" "N" "R-sq")) ///
    mtitles("Make plans" "Work goals" "Clear goals" "High standards" "Change after setback" "Work hard" "Focus on goals" "Plan to reach goal" "Keep trying after failure")

* ----------------- Table D.4 ----------------- *
* Conscientiousness traits: Responsibility and Punctuality
	
eststo clear

// Store responsability traits
local traits_count = 17
local resp_traits_count = 9
local col = 1
foreach y in $traits_res {
	local wy_index = `traits_count' + `col'
	
    eststo org_`col': reg `y' treat $X0_base $X1_mid_base `y'_base if survey_round==2&sample==1, robust
    
    listcoef
    mat A = r(table)
    sum `y' if (survey_round == 2 & treat == 0 & sample==1)
    
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd scalar StdY = A[1,5], replace
    estadd local wy_pval = "[" + string(wy_pvals[`wy_index',4], "%9.3f") + "]", replace
    
    local col = `col' + 1
}

// Store punctuality traits
local traits_count = 26
local punc_traits_count = 6
local col = 1
foreach y in $traits_pun {
    local wy_index = `traits_count' + `col'
    
    eststo ind_`col': reg `y' treat $X0_base $X1_mid_base `y'_base if survey_round==2&sample==1, robust
    
    listcoef
    mat A = r(table)
    sum `y' if (survey_round == 2 & treat == 0 & sample==1)
    
    estadd local Mean = round(`r(mean)', 0.01), replace
    estadd scalar StdY = A[1,5], replace
    estadd local wy_pval = "[" + string(wy_pvals[`wy_index',4], "%9.3f") + "]", replace
    
    local col = `col' + 1
}

* Assemble table --------

esttab org_* using "$outputs/tableD4_consci-resp-punc.tex", fragment replace ///
    prehead("{" "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" ///
            "\begin{tabular*}{2\textwidth}{l*{`resp_traits_count'}{c}}" "\toprule" "\multicolumn{@span}{l}{Panel A: Responsibility} \\") ///
	postfoot("\end{tabular*}") ///
    compress nocons se varlabels(_cons "Constant" treat "Treated") ///
    drop(trait_*_base sex_base age_base educ_high_base hh_n_bedrooms_base hh_n_children_base hh_n_above_18_base total_* hhincome* ) ///
    booktabs star(* 0.10 ** 0.05 *** 0.01 ) nonotes b(3) se(3) ///
    s(wy_pval StdY Mean N r2, fmt(%9s %9.3f %9.2f %9.0f %9.3f) ///
	label("WY adj. p-value" "Standardized coeff." "Mean outcome control" "N" "R-sq")) ///
    mtitles("Present for others" "Dependable" "Keep promises" "Fullfill engagements" "Up to responsibilities" "Respect engagements" "Others count on me" "Fulfill duty" "Manage responsibilities")

esttab ind_* using "$outputs/tableD4_consci-resp-punc.tex", fragment append ///
	prehead("\begin{tabular*}{2\textwidth}{l*{`punc_traits_count'}{c}}" "\toprule" "\multicolumn{@span}{l}{Panel B: Punctuality} \\") ///
	postfoot("\bottomrule" "\end{tabular*}" "}") ///
    compress nocons se varlabels(_cons "Constant" treat "Treated") ///
    drop(trait_*_base sex_base age_base educ_high_base hh_n_bedrooms_base hh_n_children_base hh_n_above_18_base total_* hhincome* ) ///
    booktabs star(* 0.10 ** 0.05 *** 0.01 ) nonotes b(3) se(3) ///
    s(wy_pval StdY Mean N r2, fmt(%9s %9.3f %9.2f %9.0f %9.3f) ///
	label("WY adj. p-value" "Standardized coeff." "Mean outcome control" "N" "R-sq")) ///
    mtitles("Remorse late" "Meet earlier" "Avoid being late" "Avoid procrastination" "Punctual" "On time at meetings")

* ----------------- Table D.5 ----------------- *
* Big 5: Extroversion, Agreeableness, Emotional stability and Openness

eststo clear
local traits_count = 32
local col = 1
foreach y in $big5_avg{

    local wy_index = `traits_count' + `col'

    eststo: reg `y' treat $X0_base $X1_mid_base `y'_base if survey_round==2 & sample==1, robust
    listcoef
    mat A = r(table)
    sum	`y' if (survey_round == 2 & treat == 0 & sample==1)
        estadd local Mean = round(`r(mean)', 0.01), replace
        estadd local Base "\checkmark", replace
        estadd local BaseO "\checkmark", replace
        estadd scalar StdY = A[1,5], replace
        estadd local wy_pval = "[" + string(wy_pvals[`wy_index',4], "%9.3f") + "]", replace
		
	local col = `col' + 1
}

esttab *
esttab * using "$outputs/tableD5_big5.tex", replace ///
	compress nocons se varlabels(_cons "Constant" treat "Treated") ///
	drop(big*_base sex_base age_base educ_high_base hh_n_bedrooms_base hh_n_children_base hh_n_above_18_base total_* hhincome* ) ///
	booktabs star(* 0.10 ** 0.05 *** 0.01 ) nonotes b(3) se(3) ///
	s(wy_pval StdY Mean Base BaseO N r2, fmt(%9s %9.3f %9.3f %9s %9s %9.0f %9.3f) ///
	label("WY adj. p-value" "Standardized coeff." "Mean outcome control" "Baseline controls" "Trait at baseline" "N" "R-sq")) ///
	mtitles("Extrovert" "Agreeable" "Emotional stability" "Openness to experience")

eststo	clear

********************************************************************************
*  APPENDIX OUTPUTS E
********************************************************************************

* ----------------- Table E.1 ----------------- *
* COVID-19 outcomes: bivariate

wyoung $covid_outcomes, cmd(reg OUTCOMEVAR treat if survey_round==3 & sample==1, r) familyp(treat) bootstraps(1000) seed($seed)
matrix wy_pvals = r(table)

eststo clear

local col = 1
foreach y in $covid_outcomes {

    eststo: reg `y' treat if survey_round==3 & sample==1, robust
    listcoef
    matrix A = r(table)
    estadd local Base ""
    estadd scalar StdY = A[1,5]
    estadd local wy_pval = "[" + string(wy_pvals[`col',4], "%9.3f") + "]", replace

    local col = `col' + 1
}

esttab est1 est2 est3 est4 est5 est6 est7 using "$outputs/tableE1_covid-outcomes-bivariate.tex", ///
    fragment replace ///
    prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" ///
            "\begin{tabular*}{\textwidth}{l*{@span}{c}}" ///
            "\toprule" ///
            "\multicolumn{@span}{l}{\textit{Panel A}} \\") ///
    compress varlabels(_cons "Constant" treat "Treated") ///
    star(* 0.10 ** 0.05 *** 0.01) nonotes nonum b(3) se(3) ///
    s(wy_pval StdY N r2, fmt(%9s %9.3f %9.0f %9.3f) ///
    label("WY adj. p-value" "Std. coeff." "N" "R-sq")) ///
    mtitles("Protective measures" "Effective measures" "Display symptoms" "Contract change" "Lost income" "Income decrease" "HH income decrease")

esttab est8 est9 est10 est11 est12 est13 est14 using "$outputs/tableE1_covid-outcomes-bivariate.tex", ///
    fragment append ///
    prehead("\midrule" ///
            "\multicolumn{@span}{l}{\textit{Panel B}} \\") ///
    postfoot("\bottomrule" ///
             "\end{tabular*}" ///
             "}") ///
    compress varlabels(_cons "Constant" treat "Treated") ///
    star(* 0.10 ** 0.05 *** 0.01) nonotes nonum b(3) se(3) ///
    s(wy_pval StdY N r2, fmt(%9s %9.3f %9.0f %9.3f) ///
    label("WY adj. p-value" "Std. coeff." "N" "R-sq")) ///
    mtitles("Transfers received decrease" "Food expend. increase" "Health expend. increase" "Educ expend. increase" "Transfers sent increase" "Savings decrease" "Borrowing increase")

eststo clear

* ----------------- Table E.2 ----------------- *
* COVID-19 outcomes: full controls

wyoung $covid_outcomes, cmd(reg OUTCOMEVAR treat $X0_base $X1_mid_base if survey_round==3 & sample==1, r) familyp(treat) bootstraps(1000) seed($seed)
matrix wy_pvals = r(table)

eststo clear

local col = 1
foreach y in $covid_outcomes {

    eststo: reg `y' treat $X0_base $X1_mid_base if survey_round==3 & sample==1, robust
    listcoef
    matrix A = r(table)
    estadd local Base "\checkmark"
    estadd scalar StdY = A[1,5]
    estadd local wy_pval = "[" + string(wy_pvals[`col',4], "%9.3f") + "]", replace

    local col = `col' + 1
}

esttab est1 est2 est3 est4 est5 est6 est7 using "$outputs/tableE2_covid-outcomes-controls.tex", ///
    fragment replace ///
    prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" ///
            "\begin{tabular*}{\textwidth}{l*{@span}{c}}" ///
            "\toprule" ///
            "\multicolumn{@span}{l}{\textit{Panel A}} \\") ///
    compress varlabels(_cons "Constant" treat "Treated") ///
    star(* 0.10 ** 0.05 *** 0.01) nonotes nonum b(3) se(3) ///
	drop(sex_base age_base educ_high_base hh_n_bedrooms_base hh_n_children_base hh_n_above_18_base total_* hhincome* ) ///
    s(wy_pval StdY Base N r2, fmt(%9s %9.3f %9s %9.0f %9.3f) ///
    label("WY adj. p-value" "Std. coeff." "Basel. controls" "N" "R-sq")) ///
    mtitles("Protective measures" "Effective measures" "Display symptoms" "Contract change" "Lost income" "Income decrease" "HH income decrease")

esttab est8 est9 est10 est11 est12 est13 est14 using "$outputs/tableE2_covid-outcomes-controls.tex", ///
    fragment append ///
    prehead("\midrule" ///
            "\multicolumn{@span}{l}{\textit{Panel B}} \\") ///
    postfoot("\bottomrule" ///
             "\end{tabular*}" ///
             "}") ///
    compress varlabels(_cons "Constant" treat "Treated") ///
    star(* 0.10 ** 0.05 *** 0.01) nonotes nonum b(3) se(3) ///
	drop(sex_base age_base educ_high_base hh_n_bedrooms_base hh_n_children_base hh_n_above_18_base total_* hhincome* ) ///
    s(wy_pval StdY Base N r2, fmt(%9s %9.3f %9s %9.0f %9.3f) ///
    label("WY adj. p-value" "Std. coeff." "Basel. controls" "N" "R-sq")) ///
    mtitles("Transfers received decrease" "Food expend. increase" "Health expend. increase" "Educ expend. increase" "Transfers sent increase" "Savings decrease" "Borrowing increase")

eststo clear

********************************************************************************
*  REPLICATION SCRIPT COMPLETED
********************************************************************************

display "Replication script completed"
display "Output directory: $outputs"

********************************************************************************
*  END
********************************************************************************
